<?php
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
#	FILE:			includes/userconstruct.php
#	FUNCTION:		user -related class
#	AUTHOR:			Adrian Liu
#	CREATED:		21/02/2012
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

class userconstruct{

	private $id;
	private $login_name;
	private $first_name;
	private $last_name;
	private $group;
	private $editable_groups;
	private $hub_page;
	private $password;
	private $permission;
	private $visited_pages;
	private $can_lock;
	private $can_approve;
	private $email;
	private $newsletter_groups;
	
	/**
	*construtor of this class
	*
	*/
	function __construct() {
			
		$this -> id = "";
		$this -> login_name = "";
		$this -> first_name = "";
		$this -> last_name = "";
		$this -> group = NULL;
		$this -> editable_groups = NULL;
		$this -> hub_page = "";
		$this -> password = "";
		$this -> permission = "";
		$this -> visited_pages = array();
		$this -> can_lock = "";
		$this -> can_approve = "";
		$this -> email = "";
		$this -> newsletter_groups = NULL;
	}
   
   	/**
	*destructor of this class
	*
	*/
   	function __destruct(){
		
		#destroy
		unset($this -> id);
		unset($this -> login_name);
		unset($this -> first_name);
		unset($this -> last_name);
		unset($this -> group);
		unset($this -> editable_groups);
		unset($this -> hub_page);
		unset($this -> password);
		unset($this -> permission);
		unset($this -> visited_pages);
		unset($this -> can_lock);
		unset($this -> can_approve);
		unset($this -> email);
		unset($this -> newsletter_groups);
   	}
	
	
	#user id
	#----------------------------------------------
	/**
	*loadID() load given id to class
	*@param $id is given id
	*/
	protected function loadID($id){
		
		$this -> id = $id;
	}
	
	/**
	*getUserID() get user id and return it
	*/
	protected function getUserID(){
		
		return $this -> id;
	}
	#------------------------------------------------
	
	#login name
	#------------------------------------------------
	/**
	*loadUserLoginName() load given user name to class
	*@param $userName is given user login name
	*/
	protected function loadUserLoginName($userName){
		
		$this -> login_name = $userName;
	}
	
	/**
	*getUserLoginName() return user login name
	*/
	protected function getUserLoginName(){
		
		return $this -> login_name;
	}
	#----------------------------------------------------
	
	#first name
	#---------------------------------------------------
	/**
	*loadFirstName() load first name to class
	*@param $firstName is first name of this user
	*/
	protected function loadFirstName($firstName){
		
		$this -> first_name = $firstName;
	}
	
	/**
	*getUserFirstName() get first user name and return it
	*
	*/
	protected function getUserFirstName(){
		
		return $this -> first_name;
	}
	
	#----------------------------------------------------
	
	
	#last name
	#---------------------------------------------------
	/**
	*loadLastName() load last name to class
	*@param $lastName is the last name of this user
	*/
	protected function loadLastName($lastName){
		
		$this -> last_name = $lastName;
	}
	
	/**
	*getUserLastName() get last name of this user
	*/
	protected function getUserLastName(){
		
		return $this -> last_name;
	}
	#----------------------------------------------------
	
	#user group
	#---------------------------------------------------
	/**
	*loadUserGroup() load user group to class
	*@param $group is the group of this user
	*/
	protected function loadUserGroup($group){
		
		$this -> group = $group;
	}
	
	/**
	*getUserGroup() get user group of this user
	*/
	public function getUserGroup(){
		
		return $this -> group;
	}
	#-----------------------------------------------------
	
	#hub page
	#-----------------------------------------------------
	/**
	*loadHubPage() load hub page to class
	*@param $hubPage is the hubPage of this user
	*/
	protected function loadHubPage($hubPage){
		
		$this -> hub_page = $hubPage;
	}
	
	/**
	*getHubPage() get hub page of this user
	*/
	protected function getHubPage(){
		
		return $this -> hub_page;
	}
	#-------------------------------------------------------
	
	#password
	#-----------------------------------------------------
	/**
	*loadPassWord() load given password to class
	*@param $passWord is given user password
	*/
	protected function loadPassWord($passWord){
		
		$this -> password = $passWord;
	}
	
	/**
	*getPassWord() return user password
	*/
	protected function getPassWord(){
		
		return $this -> password;
	}
	#----------------------------------------------------
	
	#Visited Pages
	#----------------------------------------------------
	/**
	*loadVisitedPages() load visited pages to class
	*@param $visitedPage is the pages which current has visited
	*/
	protected function loadVisitedPages($visitedPage){
		
		$this -> visited_pages = $visitedPage;
	}
	
	/**
	*getVisitedPages() return pages which has been visited by this user
	*/
	protected function getVisitedPages(){
		
		return $this -> visited_pages;
	}
	#------------------------------------------------------
	
	#Email
	#-------------------------------------------------------
	/**
	*loadEmailAddress() load given email address to this class
	*@param $email is given email
	*/
	protected function loadEmailAddress($email){
		
		$this -> email = $email;
	}
	
	/**
	*getEmailAddress() get email address and return it
	*/
	protected function getEmailAddress(){
		
		return $this -> email;
	}
	#-------------------------------------------------------
	
	#Newsletter Groups
	#-------------------------------------------------------
	/**
	*loadNewsletterGroup() load given newslettergroup to class and return it
	*@param $newsletter_groups is given newsletter group
	*/
	protected function loadNewsletterGroup($newsletter_groups){
		
		$this -> newsletter_groups =  $newsletter_groups;
	}
	
	/**
	*getNewsletterGroup() get newsletter group and return it
	*/
	public function getNewsletterGroup(){	
		
		return $this -> newsletter_groups;
	}
	#-------------------------------------------------------
	
	//#Editable Groups
//	protected function loadEditableGroup($editable_groups){
//		
//		$this -> editable_groups = $editable_groups;
//	}
//	
	//
	public function getUserGroupKeys(){
		
		if($this -> getUserGroup() == NULL){
			
			return false;
		}
		else{
			
			return array_keys($this -> getUserGroup() -> getGroupArray());
		}

	}

	public function getEditableGroup(){
		
		if(is_null($this -> group)){
			
			return false;
		}
		else{
			
			if(count($this -> group -> getGroupArray()) == 0){
				
				return false;
			}
			else{
				
				$editableGroup = array();
				
				foreach($this -> group -> getGroupArray() as $obGroup){
					
					$hashTable = array_combine($obGroup -> getEditGroup(),$obGroup -> getEditGroup());
					
					if(count($editableGroup)==0){
						
						$editableGroup = $hashTable;
					}
					else{
					
						$editableGroup = array_merge($editableGroup,$hashTable);
					}
				}
				
				return $editableGroup;
			}
		}
	}
	
//	protected function getCanLock(){
//		
//		return $this -> can_lock;
//	}
//	
	/**
	*userLoader() this function is operating user loading function
	*@param $arrUser is array which returned by database request for user
	*/
	public function userLoader($arrUser){
		
		foreach($arrUser as $itemUser){
		
			$this -> initializeUser($itemUser);
					
		}
	}
	
	/**
	*
	*
	*/
	public function initializeUser($arrUser){
		
		$flag = false;//flag to show whether current user exist
 
		#get user id from given user array
		if(array_key_exists('u_id',$arrUser)){
			
			if($this -> id == $arrUser['u_id']){
				
				$flag = true;
			}
			else{
				
				$this -> id = $arrUser['u_id'];
			}
		}
			
		if(array_key_exists('u_login_name',$arrUser) && !$flag)
			$this -> login_name = $arrUser['u_login_name'];
		
		#get user first name from given user array
		if(array_key_exists('u_fname',$arrUser) && !$flag)
			$this -> first_name = $arrUser['u_fname'];
		
		#get user last name from given user array
		if(array_key_exists('u_lname',$arrUser) && !$flag)
			$this -> last_name = $arrUser['u_lname'];
		
		#get created date of current user
		if(array_key_exists('u_created',$arrUser) && !$flag)
			$this -> date_created = $arrUser['u_created'];
		
		#get modified date of current user	
		if(array_key_exists('u_modified',$arrUser) && !$flag)
			$this -> date_modified = $arrUser['u_modified'];
			
		if(array_key_exists('u_modified',$arrUser) && !$flag)
			$this -> email = $arrUser['u_modified'];

		#find group id of current user
		if(array_key_exists('g_id',$arrUser)){

			if(is_null($this -> group)){

				$objGroups = new Groups();
				
				$objGroups ->addGroup($this -> id,$arrUser);
				
				$this -> group = $objGroups;
			}
			else{
				
				$this -> group -> addGroup($this -> id,$arrUser);
			}
		}
		
		if(array_key_exists('ng_id',$arrUser)){
			
			if(is_null($this -> newsletter_groups)){
				
				$objGroups = new Groups();
				
				$objGroups ->addNewsletterGroup($this -> id,$arrUser);
				
				$this -> newsletter_groups = $objGroups;
			}
			else{
				
				$this -> newsletter_groups -> addNewsletterGroup($this -> id,$arrUser);
			}	
		}
	}
	
	public function getUserCreatedDate(){
		
		return $this -> date_created;
	}
	
	public function getUserModifiedDate(){
		
		return $this -> date_modified;
	}
	
}

?>